package com.dianrui.agent.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.FinanceList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StatementAdapter extends BaseAdapter {

    Context context;
    private List<FinanceList> financeList;
    StatementClickListener statementClickListener;

    public StatementAdapter(Context context, List<FinanceList> financeList) {
        this.context = context;
        this.financeList = financeList;
    }

    public void refresh(List<FinanceList> financeLists) {
        this.financeList = financeLists;
        this.notifyDataSetChanged();
    }

    public interface StatementClickListener {
        void onOnclick(String orderCount,String createTime,String areaoutside,String networkside,String money,String discount,String wxfee,String alifee,String total,String noPay, String overtimePayment);

        void onOnLastclick(String refundCard,String buyCard,String violationPrice,String orderCount,int postion,List<FinanceList> financeList,String date,String time,String total,String money,String discount,String noPay,String overtimePayment,String payFee);

    }

    public void setOnStatementClickListener(StatementClickListener statementClickListener) {
        this.statementClickListener = statementClickListener;
    }

    @Override
    public int getCount() {
        return financeList.size();
    }

    @Override
    public Object getItem(int position) {
        return financeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_statement, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (financeList.size() > 0) {
            final FinanceList financeBean = financeList.get(position);
            holder.date.setText(financeBean.create_time);
            holder.orderCount.setText(financeBean.order_count);
            holder.payFee.setText(financeBean.buy_card);
            holder.total.setText(financeBean.total);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (financeList.size()-1==position) {
                        if (statementClickListener != null) {
                            statementClickListener.onOnLastclick(financeBean.refund_card,financeBean.buy_card,financeBean.violation_price,financeBean.order_count,position,financeList,financeBean.create_time,financeBean.month,financeBean.total,financeBean.money,financeBean.discount,financeBean.no_pay,financeBean.overtime_payment,financeBean.pay_fee);
                        }
                    }else {
                        if (statementClickListener != null) {
                            statementClickListener.onOnclick(financeBean.order_count,financeBean.create_time,financeBean.area_outside,financeBean.network_outside,financeBean.money,financeBean.discount,financeBean.wx_pay_fee,financeBean.ali_pay_fee,financeBean.total,financeBean.no_pay,financeBean.overtime_payment);
                        }
                    }


                }
            });

        }

        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.date)
        TextView date;
        @BindView(R.id.order_count)
        TextView orderCount;
        @BindView(R.id.pay_fee)
        TextView payFee;
        @BindView(R.id.total)
        TextView total;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
